<?php
defined('APP_ROOT') || define('APP_ROOT', dirname(__FILE__));
require_once APP_ROOT.'/base.php';

use QL\QueryList;

class film_content extends  Base{
    /**
     * ed2000电影内容采集
     */
    public function ed2000(){
        $args = func_get_args();
        echo $args[0]['info']['url'],"\n";

        preg_match('/^(https?:\/\/)?[\d\w]{0,}\.?[\d\w]+\.\w+\/?/i',$args[0]['info']['url'],$baseUrl);
        $site_info = $this->params[rtrim($baseUrl[0],'/')];

        $content_reg = $site_info['content_reg'];

        $content = QueryList::Query($args[0]['content'],['content'=>[$content_reg['content'][0],$content_reg['content'][1],'',function($content){
            return preg_replace("/\s{2,}/",'',$content);
        }]])->getData();


        $sources = array();

        $html = preg_replace("/\s+/",'',$args[0]['content']);
        preg_match_all($content_reg['link'][0],$html,$link_preg_res);
        if($link_preg_res[1]){
            foreach($link_preg_res[1] as $key=>$value){
                if(!($value = $this->chekcSourceLinks($value,$baseUrl[0],$args[0]['info']['url']))){
                    continue;
                }
                $sources[] = $value;
            }
        }else{
            $sources = QueryList::Query($args[0]['content'],array(
                'link'=>$content_reg['link'][1]
            ))->getData();
            foreach($sources as $key=>$value){
                if(!($value = $this->chekcSourceLinks($value['link'],$baseUrl[0],$args[0]['info']['url']))){
                    continue;
                }
                $sources[] = $value;
            }
        }

        $this->insertFilm($sources,$args,$content[0]['content'],$site_info);
    }

    /**
     * 迅播网内容采集
     */
    public function xunbo(){
        $args = func_get_args();
        echo $args[0]['info']['url'],"\n";
        $args[0]['content'] = mb_convert_encoding($args[0]['content'],'utf-8','gb2312');

        preg_match('/^(https?:\/\/)?[\d\w]{0,}\.?[\d\w]+\.\w+\/?/i',$args[0]['info']['url'],$baseUrl);
        $site_info = $this->params[rtrim($baseUrl[0],'/')];

        $content_reg = $site_info['content_reg'];

        $content = QueryList::Query($args[0]['content'],['content'=>[$content_reg['content'][0],$content_reg['content'][1],'p br',function($content){
            return preg_replace("/\s{2,}/",'',$content);
        }]])->getData();



        $html = preg_replace("/\r\n/",'',$args[0]['content']);
        preg_match_all($content_reg['link'],$html,$link_preg_res);

        $sources = array();
        if($link_preg_res[1]){
            $sources = explode('###',$link_preg_res[1][0]);
            foreach($sources as $key=>$value){
                if(!($value = $this->chekcSourceLinks($value,$baseUrl[0],$args[0]['info']['url']))){
                    continue;
                }
                $sources[$key] = $value;
            }
        }

        $this->insertFilm($sources,$args,$content[0]['content'],$site_info);
    }

    /**
     * 通用内容采集
     */
    public function gather_callback_content(){
        $args = func_get_args();
        echo $args[0]['info']['url'],"\n";

        preg_match('/^(https?:\/\/)?[\d\w]{0,}\.?[\d\w]+\.\w+\/?/i',$args[0]['info']['url'],$baseUrl);
        $site_info = $this->params[rtrim($baseUrl[0],'/')];

        if(strtolower($site_info['charset']) != 'utf-8'){
            $args[0]['content'] = mb_convert_encoding($args[0]['content'],'utf-8',strtolower($site_info['charset']));
        }

        $content_reg = $site_info['content_reg'];

        $sources = QueryList::Query($args[0]['content'],['source'=>$content_reg['link']])->getData();
        foreach($sources as $key=>$value){
            if($value['source'] = $this->chekcSourceLinks($value['source'],$baseUrl[0],$args[0]['info']['url'])) {
                $sources[$key] = $value['source'];
            }else{
                unset($sources[$key]);
            }
        }

        $content = QueryList::Query($args[0]['content'],['content'=>[$content_reg['content'][0],$content_reg['content'][1],'p br',function($content){
            return preg_replace("/\s{2,}/",'',$content);
        }]])->getData();
        $this->insertFilm($sources,$args,$content[0]['content'],$site_info);
    }
}
/*$content = QueryList::Query("http://www.piaohua.com/html/dongzuo/index.html",[
   'content'=>[".page.tk a:contains('下一页')","href"],
])->getData();
var_dump($content);exit();*/
/*echo json_encode([
    'content'=>[".page.tk a:contains('下一页')","href"],
]);exit();*/
$gather = new film_content();
$site = $gather->get_film_site_url();

$gather->fuck($site);
$gather->start();

